package com.example.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.home.activity.R;
import com.example.home.viewmodel.home.CouponList;

import java.util.List;

public class HomeCouponAdapter extends BaseAdapter {

    private Context ctx;
    private List<CouponList> couponList;

    public HomeCouponAdapter(Context ctx, List<CouponList> couponList) {
        this.ctx = ctx;
        this.couponList = couponList;
    }

    @Override
    public int getCount() {
        return this.couponList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.couponList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.couponList.get(position).getId();
    }

    @Override
    public View getView(int index, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.home_coupon_item,null);
            viewHolder.home_coupon_1 = convertView.findViewById(R.id.home_coupon_1);
            viewHolder.home_coupon_2 = convertView.findViewById(R.id.home_coupon_2);
            viewHolder.home_coupon_3 = convertView.findViewById(R.id.home_coupon_3);
            viewHolder.home_coupon_4 = convertView.findViewById(R.id.home_coupon_4);
            viewHolder.home_coupon_5 = convertView.findViewById(R.id.home_coupon_5);
            convertView.setTag(viewHolder);

        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.home_coupon_1.setText(this.couponList.get(index).getDiscount()+"元");
        viewHolder.home_coupon_2.setText(this.couponList.get(index).getName());
        viewHolder.home_coupon_3.setText(this.couponList.get(index).getDesc());
        viewHolder.home_coupon_4.setText(this.couponList.get(index).getTag());
        viewHolder.home_coupon_5.setText("有效期："+this.couponList.get(index).getDays());
        return convertView;
    }

    static class ViewHolder {
        TextView home_coupon_1;
        TextView home_coupon_2;
        TextView home_coupon_3;
        TextView home_coupon_4;
        TextView home_coupon_5;

    }
}
